package com.sinopec.android;

import java.util.ArrayList;

import com.sinopec.android.bean.Agenda;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
* @ClassName: LeaderShipAgendaAdapter
* @Description: TODO(领导日程adapter)
* @author liuwei
* @date 2012-4-18 下午03:58:56
* 
*/
public class LeaderShipAgendaAdapter extends BaseAdapter {

	public ArrayList<Agenda> listData;
	private Agenda agenda;
	private Context context;
	private LayoutInflater inflater ;
	
	public LeaderShipAgendaAdapter(Context context,ArrayList<Agenda> listData){
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.listData = listData;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(listData!=null){
			return listData.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder viewHolder;
		if(convertView ==null){
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.leadership_agenda_item, null);
			viewHolder.leader = (TextView) convertView.findViewById(R.id.leadership_agenda_leader_textview);
			viewHolder.agent = (TextView) convertView.findViewById(R.id.leadership_agenda_agent_textview);
			viewHolder.time = (TextView) convertView.findViewById(R.id.leadership_agenda_time_textview);
			viewHolder.content = (TextView) convertView.findViewById(R.id.leadership_agenda_content_textview);
			viewHolder.address = (TextView) convertView.findViewById(R.id.leadership_agenda_address_textview);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(position % 2 ==0){
			convertView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.odd_item_selector));
		}else{
			convertView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.even_item_selector));
		}
		if(listData!=null){
			agenda = listData.get(position);
			viewHolder.leader.setText(agenda.getLeader());
			viewHolder.agent.setText(agenda.getAgent());
			try{
				String[] time = agenda.getTime().trim().split("至");
				viewHolder.time.setText(time[0].substring(0, time[0].lastIndexOf(":"))+"至"+time[1].substring(0, time[1].lastIndexOf(":")));
			}catch (Exception e) {
				viewHolder.time.setText(agenda.getTime());
			}
			viewHolder.content.setText(agenda.getContent());
			viewHolder.address.setText(agenda.getAddress());
		}
		
		return convertView ;
	}
	
	class ViewHolder{
		TextView leader,time,agent,content,address;
		
	}
}
